// @ts-ignore
/* eslint-disable */
import { request } from '@umijs/max';

export async function page(
  params: {
    // query
    /** 当前的页码 */
    page?: number;
    /** 页面的容量 */
    size?: number;
    formId?: any;
    keyword?: any;
    extra?: any;
  },
  options?: { [key: string]: any },
) {
  return request<{
    code: number;
    data: Record<string, any>;
    msg?: string;
  }>('/product-server/api/v1/business_data/page', {
    method: 'POST',
    data: {
      ...params,
    },
    ...(options || {}),
  });
}

export async function formField(
  params: {
    formId?: any;
  },
  options?: { [key: string]: any },
) {
  return request<{
    code: number;
    data: any[];
    msg?: string;
  }>('/product-server/api/v1/form_header/find', {
    params,
    ...(options || {}),
  });
}

export async function getForms(
  params: {
    formId?: any;
    page?: any;
    size?: any;
    keyWords?: any;
  },
  options?: { [key: string]: any },
) {
  return request<{
    code: number;
    data: Record<string, any>;
    msg?: string;
  }>('/product-server/api/v1/form_properties/page', {
    method: 'POST',
    data: params,
    ...(options || {}),
  });
}

export async function getMaxHeader(
  params: any,
  options?: { [key: string]: any },
) {
  return request<{
    code: number;
    data: any;
    msg?: string;
  }>('/product-server/api/v1/form_header/max_header_form_id', {
    data: params,
    ...(options || {}),
  });
}

export async function updateFormData(data: { [key: string]: any }, options?: { [key: string]: any }) {
  return request<{
    code: number;
    data: Record<string, any>;
    msg?: string;
  }>('/product-server/api/v1/business_data/update', {
    data,
    method: 'PUT',
    ...(options || {}),
  });
}

export async function addFormData(data: { [key: string]: any }, options?: { [key: string]: any }) {
  return request<{
    code: number;
    data: Record<string, any>;
    msg?: string;
  }>('/product-server/api/v1/business_data/save', {
    data,
    method: 'POST',
    ...(options || {}),
  });
}

export async function removeFormData(data: { idList: number[] }, options?: { [key: string]: any }) {
  return request<Record<string, any>>('/product-server/api/v1/business_data/batch_del', {
    data,
    method: 'DELETE',
    ...(options || {}),
  });
}

export async function exportExcel(params: {
  formId: any;
  fileName?: any
  sheetName?: any
}, options?: { [key: string]: any }) {
  return request<Blob>('/product-server/api/v1/excel/export', {
    params,
    responseType: 'blob',
    getResponse: true,
    ...(options || {}),
  }).then(res => {
    console.log(res)
    // console.log(res.headers['set-cookie']);
    const str = res.headers['content-disposition']
    if (!res || !str) {
      // error(res.message || '下载失败！')
      return
    }
    let fileName
    if (res?.headers['content-disposition']) {
      fileName = res?.headers['content-disposition'].replace(/\w+;filename=(.*)/, '$1')
    } else {
      fileName = params.fileName
    }
    // 此处当返回json文件时需要先对data进行JSON.stringify处理，其他类型文件不用做处理
    // const blob = new Blob([res as any], { type: headers['content-type'] })
    const dom = document.createElement('a')
    const downUrl = window.URL.createObjectURL(res?.data)
    dom.href = downUrl
    dom.download = decodeURIComponent(fileName)
    dom.style.display = 'none'
    document.body.appendChild(dom)
    dom.click()
    dom?.parentNode?.removeChild(dom)
    window.URL.revokeObjectURL(options?.url)

    return res
  })
}


export async function importExcel(data: FormData, options?: {
    [key: string]: any
}) {
  return request<{
    code: number;
    data: Record<string, any>;
    msg?: string;
  }>('/product-server/api/v1/excel/import', {
    data,
    method: 'POST',
    ...(options || {}),
  })
}

export async function downloadTemplate(params: {
  formId: any;
  fileName: any
  sheetName?: any
}, options?: { [key: string]: any }) {
  return request<Blob>('/product-server/api/v1/excel/download_temp', {
    params,
    responseType: 'blob',
    getResponse: true,
    ...(options || {}),
  }).then(res => {
    console.log(res)
    // console.log(res.headers['set-cookie']);
    const str = res.headers['content-disposition']
    if (!res || !str) {
      // error(res.message || '下载失败！')
      return
    }
    let fileName
    if (res?.headers['content-disposition']) {
      fileName = res?.headers['content-disposition'].replace(/\w+;filename=(.*)/, '$1')
    } else {
      fileName = params.fileName
    }
    // 此处当返回json文件时需要先对data进行JSON.stringify处理，其他类型文件不用做处理
    // const blob = new Blob([res as any], { type: headers['content-type'] })
    const dom = document.createElement('a')
    const downUrl = window.URL.createObjectURL(res?.data)
    dom.href = downUrl
    dom.download = decodeURIComponent(fileName)
    dom.style.display = 'none'
    document.body.appendChild(dom)
    dom.click()
    dom?.parentNode?.removeChild(dom)
    window.URL.revokeObjectURL(options?.url)

    return res
  })
}
